/*! @file sqldlg.cpp
 * @brief Implements SqlDlg Class
 * @date 23.06.2012
 */

#include "sqldlg.h"

/** @brief SqlDlg Class Constructor
 * @callgraph
 * @callergraph
 *
 * @param *parent QWidget
 */
SqlDlg::SqlDlg(QWidget *parent)
    : QDialog(parent)
{
    setupUi(this);


    sqlListTables = "SELECT name \nFROM sqlite_master \nWHERE type='table';";   //!< sqlListTables
    sqlEmpty = "SELECT * ;";                                                    //!< sqlEmpty
    sqlListViews = "SELECT *\n" \
            "FROM sqlite_master\n" \
            "WHERE type = 'view';";
    f_sqlTypedUrlSorted = "SELECT url, typed AS 'typed URLs', visit_count\n" \
            "FROM moz_historyvisits, moz_places\n" \
            "WHERE typed=1\n" \
            "AND moz_historyvisits.place_id=moz_places.id\n" \
            "ORDER BY visit_count desc;";                        //!< f_sqlTypedUrlSorted

    connect(radioButtonSql_g1, SIGNAL(clicked(bool)),
            this, SLOT(setTextG1()));
    connect(radioButtonSql_g2, SIGNAL(clicked(bool)),
            this, SLOT(setTextG2()));
    connect(radioButtonSql_g3, SIGNAL(clicked(bool)),
            this, SLOT(setTextG3()));
    connect(radioButtonSql_f1, SIGNAL(clicked(bool)),
            this, SLOT(setTextF1()));
    connect(radioButtonSql_f2, SIGNAL(clicked(bool)),
            this, SLOT(setTextF2()));
    connect(radioButtonSql_a1, SIGNAL(clicked(bool)),
            this, SLOT(setTextA1()));
    connect(pushButtonOk, SIGNAL(clicked()),
            this, SLOT(on_okButtonClicked()));
    connect(pushButtonOk, SIGNAL(clicked()),
            this, SLOT(accept()));
}

/** @brief emits okButtonClicked(QString)
 * @callgraph
 * @callergraph
 */
void SqlDlg::on_okButtonClicked()
{
    QList<QTabBar *> tabList = findChildren<QTabBar *>();
    QTabBar *tabBar = tabList.at(0);
    int currentTab = tabBar->currentIndex();

    qDebug() << "in sqldlg.cpp! currentTab:" << currentTab;

    switch (currentTab) {
    case 0:
        Q_EMIT okButtonClicked(textEditSql_g->toPlainText());
        break;
    case 1:
        Q_EMIT okButtonClicked(textEditSql_f->toPlainText());
        break;
    case 2:
        Q_EMIT okButtonClicked(textEditSql_a->toPlainText());
        break;
    }
}

/** @brief sets SQL Snippets
 * setting sql query templates
 * @callgraph
 * @callergraph
 */
void SqlDlg::setTextG1()
{
    textEditSql_g->setPlainText(sqlEmpty);
}
/** @brief sets SQL Snippets
 * @callgraph
 * @callergraph
 */
void SqlDlg::setTextG2()
{
    textEditSql_g->setPlainText(sqlListTables);
}
/** @brief sets SQL Snippets
 * @callgraph
 * @callergraph
 */
void SqlDlg::setTextG3()
{
    textEditSql_g->setPlainText(sqlListViews);
}

// set FireFox sql queries
/** @brief sets SQL Snippets
 * @callgraph
 * @callergraph
 */
void SqlDlg::setTextF1()
{
    textEditSql_f->setPlainText(sqlEmpty);
}

/** @brief sets SQL Snippets
 * @callgraph
 * @callergraph
 */
void SqlDlg::setTextF2()
{
    textEditSql_f->setPlainText(f_sqlTypedUrlSorted);
}

// set android sql queries
/** @brief sets SQL Snippets
 * @callgraph
 * @callergraph
 */
void SqlDlg::setTextA1()
{
    textEditSql_a->setPlainText(sqlEmpty);
}

